package com.sphere.freereader.ui;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphere.freereader.R;


/**
 * Created by songbangbang on 16-8-17.
 */

//TODO:  电池电量  当前时间需要接收action 根据系统的变化而变化
public class BookPageView extends LinearLayout {

    private TextView mChapterTitle;   // 章节标题

    private TextView mPageContent;   // 章节实际内容

    private TextView mCurrentTime;    // 当前系统时间

    private TextView mCurrentScale;   // 阅读进度百分比

    private View mViews;


    private int mChapterTitleSize;   // 章节title的文字大小

    private int mChapterTitleDefaultSize;

    private int mPageContentTextSize; // 章节内容文本的大小

    private int mPageContentDefaultTextSize;

    private int mCurrentTimeTextSize;

    private int mCurrentScaleTextSize;

    private int mChapterTitleTextColor;

    private int mPageContentTextColor;

    private int mCurrentTimeTextColor;

    private int mCurrentScaleTextColor;

    private int mPageTextDefaultColor;


    public View getViews() {
        return mViews;
    }

    public BookPageView(Context context) {
        super(context);
        init(context);
    }

    public BookPageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BookPageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        initViews(context);
        initRes(context);
        addView(mViews, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    private void initViews(Context context) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mViews = mInflater.inflate(R.layout.book_page_layout, null);
        mChapterTitle = (TextView) mViews.findViewById(R.id.chapter_title_tv);
        mPageContent  = (TextView) mViews.findViewById(R.id.page_content_tv);
        mCurrentTime  = (TextView) mViews.findViewById(R.id.current_time_tv);
        mCurrentScale = (TextView) mViews.findViewById(R.id.current_scale_tv);
    }

    private void initRes(Context context) {
        mChapterTitleDefaultSize = dp2sp(context, 14);
        mPageContentDefaultTextSize = dp2sp(context, 18);
        mCurrentTimeTextSize = dp2sp(context, 12);
        mCurrentScaleTextSize = dp2sp(context, 12);
        mPageTextDefaultColor = Color.BLACK;

        mCurrentTime.setTextSize(mCurrentTimeTextSize);
        mCurrentScale.setTextSize(mCurrentScaleTextSize);
        mChapterTitle.setTextSize(mChapterTitleSize != 0 ? mChapterTitleSize : mChapterTitleDefaultSize);
        mPageContent.setTextSize(mPageContentTextSize != 0 ? mPageContentTextSize : mPageContentDefaultTextSize);
        mChapterTitle.setTextColor(mChapterTitleTextColor != 0 ? mChapterTitleTextColor : mPageTextDefaultColor);
        mPageContent.setTextColor(mPageContentTextColor != 0 ? mPageContentTextColor : mPageTextDefaultColor);
        mCurrentTime.setTextColor(mCurrentTimeTextColor != 0 ? mCurrentTimeTextColor : mPageTextDefaultColor);
        mCurrentScale.setTextColor(mCurrentScaleTextColor != 0 ? mCurrentScaleTextColor : mPageTextDefaultColor);

    }

    public void setChapterTitle(CharSequence titleText) {
        if (mChapterTitle != null) {
            mChapterTitle.setText(titleText);

        }
    }

    public void setPageContent(CharSequence content) {
        if (mPageContent != null) {
            mPageContent.setText(content);

        }
    }

    public void setCurrentTime(CharSequence currentTime) {
        if (mCurrentTime != null) {
            mCurrentTime.setText(currentTime);

        }
    }

    public void setCurrentScale(CharSequence currentScale) {
        if (mCurrentScale != null) {
            mCurrentScale.setText(currentScale);
        }
    }

    public int getmChapterTitleSize() {
        return mChapterTitleSize;
    }

    public void setmChapterTitleSize(int mChapterTitleSize) {
        this.mChapterTitleSize = mChapterTitleSize;
    }

    public int getmPageContentTextSize() {
        return mPageContentTextSize;
    }

    public void setmPageContentTextSize(int mPageContentTextSize) {
        this.mPageContentTextSize = mPageContentTextSize;
    }

    public int getmCurrentTimeTextSize() {
        return mCurrentTimeTextSize;
    }

    public void setmCurrentTimeTextSize(int mCurrentTimeTextSize) {
        this.mCurrentTimeTextSize = mCurrentTimeTextSize;
    }

    public int getmCurrentScaleTextSize() {
        return mCurrentScaleTextSize;
    }

    public void setmCurrentScaleTextSize(int mCurrentScaleTextSize) {
        this.mCurrentScaleTextSize = mCurrentScaleTextSize;
    }

    public int getmChapterTitleTextColor() {
        return mChapterTitleTextColor;
    }

    public void setmChapterTitleTextColor(int mChapterTitleTextColor) {
        this.mChapterTitleTextColor = mChapterTitleTextColor;
    }

    public int getmPageContentTextColor() {
        return mPageContentTextColor;
    }

    public void setmPageContentTextColor(int mPageContentTextColor) {
        this.mPageContentTextColor = mPageContentTextColor;
    }

    public int getmCurrentTimeTextColor() {
        return mCurrentTimeTextColor;
    }

    public void setmCurrentTimeTextColor(int mCurrentTimeTextColor) {
        this.mCurrentTimeTextColor = mCurrentTimeTextColor;
    }

    public int getmCurrentScaleTextColor() {
        return mCurrentScaleTextColor;
    }

    public void setmCurrentScaleTextColor(int mCurrentScaleTextColor) {
        this.mCurrentScaleTextColor = mCurrentScaleTextColor;
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int dp2sp(Context context,float dipValue){
        return px2sp(context, dp2px(context, dipValue));
    }
}
